package action.creditapproval;

import java.sql.Timestamp;
import model.credit.Creditapplyenterprise;
import model.credit.Creditapplypersonal;
import model.creditcheck.Applyopinion;
import model.creditcheck.Applystate;
import model.security.UsrUserRole;
import service.applycheck.ApplyCheckService;
import service.applycheck.ApplyOpinionService;
import service.applycheck.ApplyStateService;
import service.applycheck.ApplyUserService;
import service.credit.CreditApplyEnterpriseService;
import service.credit.CreditApplyPersonalService;
import util.Time;
import util.Tool;
import core.action.BaseAction;
import core.global.Global;
import exception.Exceptions;

public class CreditApprovalPutAction extends BaseAction {

	private Integer applyid;
	private String setsurveyopinion;
	private Double applybaserate;
	private Double slidingscale;
	private Double performrates;
	private Integer setcheckstate;
	private String setopinion;

	private Integer custommanager;
	
	private String operateState;
	private int type;
	
	private CreditApplyPersonalService creditApplyPersonalService;
	private CreditApplyEnterpriseService creditApplyEnterpriseService;
	
	private ApplyOpinionService applyOpinionService;
	
	private ApplyStateService applyStateService;
	
	private ApplyCheckService applyCheckService;
	
	private ApplyUserService applyUserService;
	
	private Integer checkPeople;
	private String approvePeople;
	private Integer finalAP;
	
	
	/*受理*/
	public String putAcceptance() throws Exceptions{
		if(this.applyStateService.isHaveRecord(applyid)){
			this.applyStateService.delAllRecordByApplyid(applyid);
			if(this.type == 1){
				creditApplyPersonalService.resetApply(applyid);
			}else if(this.type == 2){
				creditApplyEnterpriseService.resetApply(applyid);
			}
		}
		
		if(this.type == 1){
			Creditapplypersonal _creditapplypersonal = this.creditApplyPersonalService.getOneById(applyid);
			_creditapplypersonal.setCreditstate(21);
			//_creditapplypersonal.setSurveyopinion(setsurveyopinion);
			_creditapplypersonal.setApplybaserate(applybaserate);
			_creditapplypersonal.setSlidingscale(slidingscale);
			_creditapplypersonal.setPerformrates(performrates);
			_creditapplypersonal.setIsdone(1);
			
			Timestamp nowTime = Time.CurrentTimestamp();
			//_creditapplypersonal.setAcceptdate(nowTime);
			try {
				this.creditApplyPersonalService.save(_creditapplypersonal);
				applyCheckService.saveAllByManagerId(_creditapplypersonal.getApplyid(), this.checkPeople, "A", 1);
				this.applyOpinionService.save(new Applyopinion(applyid, Global.userid().intValue(), null, "授理", setsurveyopinion, null, nowTime, 1));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}else if(this.type == 2){
			Creditapplyenterprise _creditapplyenterprise = this.creditApplyEnterpriseService.getOneById(applyid);
			_creditapplyenterprise.setCreditstate(21);
			//_creditapplyenterprise.setSurveyopinion(setsurveyopinion);
			_creditapplyenterprise.setApplybaserate(applybaserate);
			_creditapplyenterprise.setSlidingscale(slidingscale);
			_creditapplyenterprise.setPerformrates(performrates);
			_creditapplyenterprise.setIsdone(1);
			
			Timestamp nowTime = Time.CurrentTimestamp();
			//_creditapplyenterprise.setAcceptdate(nowTime);
			try {
				this.creditApplyEnterpriseService.save(_creditapplyenterprise);
				applyCheckService.saveAllByManagerId(_creditapplyenterprise.getApplyid(), this.checkPeople, "A", 2);
				this.applyOpinionService.save(new Applyopinion(applyid, Global.userid().intValue(), null, "授理", setsurveyopinion, null, nowTime, 2));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}
		MessageJump("信息受理完成", "creditApprovalAction?operateState=" + this.operateState + "&type=" + this.type);
		return "message";
	}

	
	/*审查*/
	public String putExamine() throws Exceptions {
		String result = "";
		String[] ps = Tool.splitArrayHtml(this.approvePeople);
		if(this.type == 1){
			Creditapplypersonal _creditapplypersonal = this.creditApplyPersonalService.getOneById(applyid);
			Applystate _applystate = null;
			if(applyCheckService.getOneApplyStateByIdAndType(applyid, "A") != null){
				_applystate = applyCheckService.getOneApplyStateByIdAndType(applyid, "A").get(0);
			}
			try {
				if(this.setcheckstate == 1){
					result = "同意";
					_creditapplypersonal.setCreditstate(31);
					_creditapplypersonal.setIsdone(2);
					for(int i = 0; i < ps.length; i++){
						applyCheckService.saveAllByManagerId(_creditapplypersonal.getApplyid(), Integer.parseInt(ps[i]), "B", 1);
					}
					
				}else if(this.setcheckstate == 2){
					result = "不同意";
					_creditapplypersonal.setCreditstate(12);
					_creditapplypersonal.setIsdone(2);
				}else if(this.setcheckstate == 3){
					result = "回退";
					_creditapplypersonal.setCreditstate(12);
				}
				
				_applystate.setCheckopinion(setopinion);
				_applystate.setCheckstate(setcheckstate);
				Timestamp nowTime = Time.CurrentTimestamp();
				_applystate.setChecktime(nowTime);
				creditApplyPersonalService.save(_creditapplypersonal);
				applyCheckService.save(_applystate);
				this.applyOpinionService.save(new Applyopinion(applyid, Global.userid().intValue(), null, "审查", setopinion, result, nowTime, 1));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}else if(this.type == 2){
			Creditapplyenterprise _creditapplyenterprise = this.creditApplyEnterpriseService.getOneById(applyid);
			Applystate _applystate = applyCheckService.getOneApplyStateByIdAndType(applyid, "A").get(0);
			try {
				if(this.setcheckstate == 1){
					result = "同意";
					_creditapplyenterprise.setCreditstate(31);
					_creditapplyenterprise.setIsdone(2);
					for(int i = 0; i < ps.length; i++){
						applyCheckService.saveAllByManagerId(_creditapplyenterprise.getApplyid(), Integer.parseInt(ps[i]), "B", 2);
					}
				}else if(this.setcheckstate == 2){
					result = "不同意";
					_creditapplyenterprise.setCreditstate(12);
					_creditapplyenterprise.setIsdone(2);
				}
				else if(this.setcheckstate == 3){
					result = "回退";
					_creditapplyenterprise.setCreditstate(12);
				}
				_applystate.setCheckstate(setcheckstate);
				_applystate.setCheckopinion(setopinion);
				Timestamp nowTime = Time.CurrentTimestamp();
				_applystate.setChecktime(nowTime);
				creditApplyEnterpriseService.save(_creditapplyenterprise);
				applyCheckService.save(_applystate);
				this.applyOpinionService.save(new Applyopinion(applyid, Global.userid().intValue(), null, "审查", setopinion, result, nowTime, 2));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}
		MessageJump("信息审查完成", "creditApprovalAction?operateState=" + this.operateState + "&type=" + this.type);
		return "message";
	}
	
	
	/*审批*/
	public String putApproval() throws Exceptions {
		UsrUserRole user = this.applyUserService.getOneByUserRoleId(3);
		String result = "";
		if(this.type == 1){
			Creditapplypersonal _creditapplypersonal = this.creditApplyPersonalService.getOneById(applyid);
			Applystate _applystate = applyCheckService.getOneApplyStateByIdAndType(applyid, "B").get(0);
			try {
				if(this.setcheckstate == 1){
					result = "同意";
					if(applyCheckService.isHaveFP(applyid) == 0){
						applyCheckService.saveAllByManagerId(_creditapplypersonal.getApplyid(), user.getUsrUser().getUserid().intValue(), "C", 1);
					}
				}else if(this.setcheckstate == 2){
					result = "不同意";
				}else if(this.setcheckstate == 3){
					result = "回退";
				}
				_applystate.setCheckstate(setcheckstate);
				_applystate.setCheckopinion(setopinion);
				Timestamp nowTime = Time.CurrentTimestamp();
				_applystate.setChecktime(nowTime);
				applyCheckService.save(_applystate);
				if(this.setcheckstate == 3){
					if(this.applyCheckService.getBackStateCheck(applyid) == 1){
						_creditapplypersonal.setCreditstate(22);
					}
				}else{
				if(this.applyCheckService.checkApplyState(applyid) == 1){
					_creditapplypersonal.setCreditstate(41);
					_creditapplypersonal.setIsdone(3);
				}else if(this.applyCheckService.checkApplyState(applyid) == 2){
					_creditapplypersonal.setCreditstate(22);
					_creditapplypersonal.setIsdone(3);
				}
				}
				creditApplyPersonalService.save(_creditapplypersonal);
				this.applyOpinionService.save(new Applyopinion(applyid, Global.userid().intValue(), null, "审批", setopinion, result, nowTime, 1));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}else if(this.type == 2){
			Creditapplyenterprise _creditapplyenterprise = this.creditApplyEnterpriseService.getOneById(applyid);
			Applystate _applystate = applyCheckService.getOneApplyStateByIdAndType(applyid, "B").get(0);
			try {
				if(this.setcheckstate == 1){
					result = "同意";
					if(applyCheckService.isHaveFP(applyid) == 0){
						applyCheckService.saveAllByManagerId(_creditapplyenterprise.getApplyid(), user.getUsrUser().getUserid().intValue(), "C", 2);
					}
				}else if(this.setcheckstate == 2){
					result = "不同意";
				}else if(this.setcheckstate == 3){
					result = "回退";
				}
				_applystate.setCheckstate(setcheckstate);
				_applystate.setCheckopinion(setopinion);
				Timestamp nowTime = Time.CurrentTimestamp();
				_applystate.setChecktime(nowTime);
				
				applyCheckService.save(_applystate);
				
				if(this.setcheckstate == 3){
					if(this.applyCheckService.getBackStateCheck(applyid) == 1){
						_creditapplyenterprise.setCreditstate(22);
					}
				}else{
				if(this.applyCheckService.checkApplyState(applyid) == 1){
					_creditapplyenterprise.setCreditstate(41);
					_creditapplyenterprise.setIsdone(3);
				}else if(this.applyCheckService.checkApplyState(applyid) == 2){
					_creditapplyenterprise.setCreditstate(22);
					_creditapplyenterprise.setIsdone(3);
				}
				}
				creditApplyEnterpriseService.save(_creditapplyenterprise);
				this.applyOpinionService.save(new Applyopinion(applyid, Global.userid().intValue(), null, "审批", setopinion, result, nowTime, 2));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}
		
		MessageJump("信息审核完成", "creditApprovalAction?operateState=" + this.operateState + "&type=" + this.type);
		return "message";
	}

	
	/*终审*/
	public String putFinalApproval() throws Exceptions {
		String result = "";
		if(this.type == 1){
			Creditapplypersonal _creditapplypersonal = this.creditApplyPersonalService.getOneById(applyid);
			Applystate _applystate = applyCheckService.getOneApplyStateByIdAndType(applyid, "C").get(0);
			try {
				if(this.setcheckstate == 1){
					result = "同意";
					_creditapplypersonal.setCreditstate(10);
					_creditapplypersonal.setIsdone(4);
				}else if(this.setcheckstate == 2){
					result = "不同意";
					_creditapplypersonal.setCreditstate(32);
					_creditapplypersonal.setIsdone(4);
				}
				_applystate.setCheckopinion(setopinion);
				_applystate.setCheckstate(setcheckstate);
				Timestamp nowTime = Time.CurrentTimestamp();
				_applystate.setChecktime(nowTime);
				creditApplyPersonalService.save(_creditapplypersonal);
				//System.out.println(_creditapplypersonal);
				applyCheckService.save(_applystate);
				this.applyOpinionService.save(new Applyopinion(applyid, Global.userid().intValue(), null, "终审", setopinion, result, nowTime, 1));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}else if(this.type == 2){
			Creditapplyenterprise _creditapplyenterprise = this.creditApplyEnterpriseService.getOneById(applyid);
			Applystate _applystate = applyCheckService.getOneApplyStateByIdAndType(applyid, "C").get(0);
			try {
				if(this.setcheckstate == 1){
					result = "同意";
					_creditapplyenterprise.setCreditstate(10);
					_creditapplyenterprise.setIsdone(4);
				}else if(this.setcheckstate == 2){
					result = "不同意";
					_creditapplyenterprise.setCreditstate(32);
					_creditapplyenterprise.setIsdone(4);
				}
				_applystate.setCheckstate(setcheckstate);
				_applystate.setCheckopinion(setopinion);
				Timestamp nowTime = Time.CurrentTimestamp();
				_applystate.setChecktime(nowTime);
				creditApplyEnterpriseService.save(_creditapplyenterprise);
				applyCheckService.save(_applystate);
				this.applyOpinionService.save(new Applyopinion(applyid, Global.userid().intValue(), null, "终审", setopinion, result, nowTime, 2));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}
		MessageJump("信息最终审核完成", "creditApprovalAction?operateState=" + this.operateState + "&type=" + this.type);
		return "message";
	}

	public Integer getApplyid() {
		return applyid;
	}

	public void setApplyid(Integer applyid) {
		this.applyid = applyid;
	}

	public String getSetsurveyopinion() {
		return setsurveyopinion;
	}

	public void setSetsurveyopinion(String setsurveyopinion) {
		this.setsurveyopinion = setsurveyopinion;
	}

	@Override
	public String toString() {
		return "CreditApprovalPutAction [applyid=" + applyid + ", setsurveyopinion="
				+ setsurveyopinion + ", baserate=" + applybaserate
				+ ", slidingscale=" + slidingscale + ", performrates="
				+ performrates + ", custommanager=" + custommanager
				+ ", operateState=" + operateState + ", type=" + type
				+ ", creditApplyPersonalService=" + creditApplyPersonalService
				+ ", creditApplyEnterpriseService="
				+ creditApplyEnterpriseService + ", applyCheckService="
				+ applyCheckService + "]";
	}

	public String getOperateState() {
		return operateState;
	}

	public void setOperateState(String operateState) {
		this.operateState = operateState;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public Integer getCustommanager() {
		return custommanager;
	}

	public void setCustommanager(Integer custommanager) {
		this.custommanager = custommanager;
	}

	public CreditApplyPersonalService getCreditApplyPersonalService() {
		return creditApplyPersonalService;
	}

	public void setCreditApplyPersonalService(
			CreditApplyPersonalService creditApplyPersonalService) {
		this.creditApplyPersonalService = creditApplyPersonalService;
	}

	public CreditApplyEnterpriseService getCreditApplyEnterpriseService() {
		return creditApplyEnterpriseService;
	}

	public void setCreditApplyEnterpriseService(
			CreditApplyEnterpriseService creditApplyEnterpriseService) {
		this.creditApplyEnterpriseService = creditApplyEnterpriseService;
	}

	public ApplyCheckService getApplyCheckService() {
		return applyCheckService;
	}

	public void setApplyCheckService(ApplyCheckService applyCheckService) {
		this.applyCheckService = applyCheckService;
	}

	public Double getApplybaserate() {
		return applybaserate;
	}

	public void setApplybaserate(Double applybaserate) {
		this.applybaserate = applybaserate;
	}

	public Double getSlidingscale() {
		return slidingscale;
	}

	public void setSlidingscale(Double slidingscale) {
		this.slidingscale = slidingscale;
	}

	public Double getPerformrates() {
		return performrates;
	}

	public void setPerformrates(Double performrates) {
		this.performrates = performrates;
	}

	public Integer getSetcheckstate() {
		return setcheckstate;
	}

	public void setSetcheckstate(Integer setcheckstate) {
		this.setcheckstate = setcheckstate;
	}

	public String getSetopinion() {
		return setopinion;
	}

	public void setSetopinion(String setopinion) {
		this.setopinion = setopinion;
	}

	public Integer getCheckPeople() {
		return checkPeople;
	}

	public void setCheckPeople(Integer checkPeople) {
		this.checkPeople = checkPeople;
	}

	public String getApprovePeople() {
		return approvePeople;
	}

	public void setApprovePeople(String approvePeople) {
		this.approvePeople = approvePeople;
	}

	public Integer getFinalAP() {
		return finalAP;
	}

	public void setFinalAP(Integer finalAP) {
		this.finalAP = finalAP;
	}


	public ApplyOpinionService getApplyOpinionService() {
		return applyOpinionService;
	}


	public void setApplyOpinionService(ApplyOpinionService applyOpinionService) {
		this.applyOpinionService = applyOpinionService;
	}

	public ApplyUserService getApplyUserService() {
		return applyUserService;
	}
	public void setApplyUserService(ApplyUserService applyUserService) {
		this.applyUserService = applyUserService;
	}
	public ApplyStateService getApplyStateService() {
		return applyStateService;
	}


	public void setApplyStateService(ApplyStateService applyStateService) {
		this.applyStateService = applyStateService;
	}
}
